package com.soooft.tickey.security.core.datasource;

import org.apache.commons.lang.StringUtils;

import java.util.Map;

public class DbContextHolder {
	// 线程安全的ThreadLocal
	private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
	private static final ThreadLocal<String> userIdHolder = new ThreadLocal<String>();
	private static final ThreadLocal<String> weiXinCode = new ThreadLocal<String>();
	private static final ThreadLocal<Map<String,Object>> weiXinUser = new ThreadLocal<Map<String,Object>>();

	private static String defaultAccountSuitName = "";

	public static void setCurrentAccountSuit(String currentAccountSuit) {
		// DbContextHolder.currentAccountSuit = currentAccountSuit;
		contextHolder.set(currentAccountSuit);
	}
	public static void setWeiXinCode(String code) {
		// DbContextHolder.currentAccountSuit = currentAccountSuit;
		weiXinCode.set(code);
	}
	public static String getWeiXinCode() {
		return ((String) weiXinCode.get());
	}
	public static void setWeiXinUser(Map<String,Object> user) {
		// DbContextHolder.currentAccountSuit = currentAccountSuit;
		weiXinUser.set(user);
	}
	public static Map<String,Object> getWeiXinUser() {
		return ((Map<String,Object>) weiXinUser.get());
	}

	
	public static String getDefaultAccountSuitName() {
		return defaultAccountSuitName!=null?defaultAccountSuitName.toLowerCase():"";
	}

	public static void setDefaultAccountSuitName(String defaultAccountSuitName) {
		DbContextHolder.defaultAccountSuitName = defaultAccountSuitName;
	}

	public static String getCurrentAccountSuit() {
		String currentAccountSuit = ((String) contextHolder.get());
		return  (StringUtils.isEmpty(currentAccountSuit)) ? getDefaultAccountSuitName() : currentAccountSuit.toLowerCase();
	}

	public static void setCurrentOperatorId(String userId) {
		userIdHolder.set(userId);
	}

	public static String getCurrentOperatorId() {
		String userId =  userIdHolder.get();
		return  (StringUtils.isEmpty(userId)) ? null : userId;
	}

}